package com.yida.gwj.deposit.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.yida.common.core.constant.SecurityConstants;
import com.yida.common.core.exception.ServiceException;
import com.yida.common.core.utils.CheckRemoteServiceResultUtils;
import com.yida.common.core.utils.DateUtils;
import com.yida.common.core.utils.StringUtils;
import com.yida.common.core.web.page.TableDataInfo;
import com.yida.common.core.web.page.TableSupport;
import com.yida.common.security.utils.SecurityUtils;
import com.yida.gwj.api.domain.GwjConfStep;
import com.yida.gwj.api.domain.GwjFinanceAccountDetail;
import com.yida.gwj.api.domain.GwjFinanceAccountMain;
import com.yida.gwj.api.domain.GwjFinanceBank;
import com.yida.gwj.api.domain.request.RemoteGwjFinanceAccountDetailRequest;
import com.yida.gwj.api.domain.request.RemoteGwjFinanceAccountMainRequest;
import com.yida.gwj.api.service.RemoteGwjConfigService;
import com.yida.gwj.api.service.RemoteGwjFinanceService;
import com.yida.gwj.common.domain.dto.GwjUserDTO;
import com.yida.gwj.common.enums.*;
import com.yida.gwj.common.utils.GwjConvertUtil;
import com.yida.gwj.deposit.domain.*;
import com.yida.gwj.deposit.domain.dto.GwjBusinessFlowDTO;
import com.yida.gwj.deposit.enums.GwjDepositBusinessBizTypeEnum;
import com.yida.gwj.deposit.mapper.GwjDepositBusinessMapper;
import com.yida.gwj.deposit.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 业务（缴存业务管理）Service业务层处理
 *
 * @author lqq
 * @date 2023-01-09
 */
@Service
public class GwjDepositBusinessServiceImpl implements IGwjDepositBusinessService {

    @Resource
    private GwjDepositBusinessMapper gwjDepositBusinessMapper;

    @Resource
    private IGwjDepositBusinessLogService gwjDepositBusinessLogService;

    @Resource
    private IGwjDepositBusinessCommunityService gwjDepositBusinessCommunityService;

    @Resource
    private IGwjDepositBusinessBuildService gwjDepositBusinessBuildService;

    @Resource
    private IGwjDepositBusinessHouseService gwjDepositBusinessHouseService;

    @Resource
    private IGwjDepositBusinessOwnerService gwjDepositBusinessOwnerService;

    @Resource
    private RemoteGwjConfigService remoteGwjConfigService;

    @Resource
    private RemoteGwjFinanceService remoteGwjFinanceService;

    @Resource
    private IGwjDepositBusinessMoneyService gwjDepositBusinessMoneyService;

    @Autowired
    private IGwjDepositBusinessDetailService gwjDepositBusinessDetailService;

    /**
     * 查询业务（缴存业务管理）
     *
     * @param bizId 业务（缴存业务管理）主键
     * @return 业务（缴存业务管理）
     */
    @Override
    public GwjDepositBusiness selectGwjDepositBusinessByBizId(Long bizId) {
        return gwjDepositBusinessMapper.selectGwjDepositBusinessByBizId(bizId);
    }

    /**
     * 查询业务（缴存业务管理）列表
     *
     * @param gwjDepositBusiness 业务（缴存业务管理）
     * @return 业务（缴存业务管理）
     */
    @Override
    public List<GwjDepositBusiness> selectGwjDepositBusinessList(GwjDepositBusiness gwjDepositBusiness) {
        return gwjDepositBusinessMapper.selectGwjDepositBusinessList(gwjDepositBusiness);
    }

    /**
     * 新增业务（缴存业务管理）
     *
     * @param gwjDepositBusiness 业务（缴存业务管理）
     * @return 结果
     */
    @Override
    public int insertGwjDepositBusiness(GwjDepositBusiness gwjDepositBusiness) {
        gwjDepositBusiness.setCreateBy(SecurityUtils.getLoginUser().getSysUser().getRealName());
        gwjDepositBusiness.setCreateTime(DateUtils.getNowDate());
        return gwjDepositBusinessMapper.insertGwjDepositBusiness(gwjDepositBusiness);
    }

    /**
     * 修改业务（缴存业务管理）
     *
     * @param gwjDepositBusiness 业务（缴存业务管理）
     * @return 结果
     */
    @Override
    public int updateGwjDepositBusiness(GwjDepositBusiness gwjDepositBusiness) {
        gwjDepositBusiness.setUpdateBy(SecurityUtils.getLoginUser().getSysUser().getRealName());
        gwjDepositBusiness.setUpdateTime(DateUtils.getNowDate());
        return gwjDepositBusinessMapper.updateGwjDepositBusiness(gwjDepositBusiness);
    }

    /**
     * 批量删除业务（缴存业务管理）
     *
     * @param bizIds 需要删除的业务（缴存业务管理）主键
     * @return 结果
     */
    @Override
    public int deleteGwjDepositBusinessByBizIds(Long[] bizIds) {
        return gwjDepositBusinessMapper.deleteGwjDepositBusinessByBizIds(bizIds);
    }

    /**
     * 删除业务（缴存业务管理）信息
     *
     * @param bizId 业务（缴存业务管理）主键
     * @return 结果
     */
    @Override
    public int deleteGwjDepositBusinessByBizId(Long bizId) {
        return gwjDepositBusinessMapper.deleteGwjDepositBusinessByBizId(bizId);
    }

    /**
     * 送件
     *
     * @param gwjDepositBusinessLog 意见请求参数
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void send(GwjDepositBusinessLog gwjDepositBusinessLog) {
        //判断当前用户是否有权限
        GwjDepositBusiness gwjDepositBusiness = checkPermission(gwjDepositBusinessLog.getBizId());
        //修改业务当前办理人员
        gwjDepositBusiness.setCurrentOperId(gwjDepositBusinessLog.getUserId());
        gwjDepositBusiness.setCurrentOperName(gwjDepositBusinessLog.getRealName());
        gwjDepositBusinessMapper.updateGwjDepositBusiness(gwjDepositBusiness);
        if (checkStepConfigInfo(gwjDepositBusiness.getStepId(), StepConfigInfoEnum.SEND.getCode())) {
            throw new ServiceException("当前步骤不允许送件");
        }
        //记录日志
        gwjDepositBusinessLogService.addSendLog(gwjDepositBusinessLog, gwjDepositBusiness);
    }

    /**
     * 签署意见
     *
     * @param gwjDepositBusinessLog 意见请求参数
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void signOpinion(GwjDepositBusinessLog gwjDepositBusinessLog) {
        //判断当前用户是否有权限
        GwjDepositBusiness gwjDepositBusiness = checkPermission(gwjDepositBusinessLog.getBizId());
        //记录日志
        gwjDepositBusinessLog.setStepId(gwjDepositBusiness.getStepId());
        if (checkStepConfigInfo(gwjDepositBusiness.getStepId(), StepConfigInfoEnum.SUBMIT_SIGN.getCode())) {
            throw new ServiceException("当前步骤不允许签署意见");
        }
        gwjDepositBusinessLogService.addSignOpinionLog(gwjDepositBusinessLog);
    }

    /**
     * 回退
     *
     * @param gwjDepositBusinessLog 意见请求参数
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void fallback(GwjDepositBusinessLog gwjDepositBusinessLog) {
        //判断当前用户是否有权限
        GwjDepositBusiness gwjDepositBusiness = checkPermission(gwjDepositBusinessLog.getBizId());
        if (checkStepConfigInfo(gwjDepositBusiness.getStepId(), StepConfigInfoEnum.FALL_BACK.getCode())) {
            throw new ServiceException("当前步骤不允许回退");
        }
        //查找上一个步骤办理人员
        GwjConfStep step = remoteGwjConfigService.selectPreviousGwjConfStepByFlagId(gwjDepositBusiness.getBizFlagId(), gwjDepositBusiness.getStepId(), SecurityConstants.INNER).getData();
        if (StringUtils.isNull(step)) {
            throw new ServiceException("当前业务步骤是第一步，不能再回退");
        }
        //如果当前步骤有生成流水，需要删除
        if (!checkStepConfigInfo(gwjDepositBusiness.getStepId(), StepConfigInfoEnum.GENERATE_FINANCIAL_STATEMENT.getCode())) {
            //删除总账流水
            if (CheckRemoteServiceResultUtils.isSuccessWithData(remoteGwjFinanceService.selectGwjFinanceAccountMainByBizId(gwjDepositBusiness.getBizId(), SecurityConstants.INNER))) {
                if (CheckRemoteServiceResultUtils.isCodeFail(remoteGwjFinanceService.deleteGwjFinanceAccountMainByBizId(gwjDepositBusiness.getBizId(), SecurityConstants.INNER))) {
                    throw new ServiceException("删除总账流水失败");
                }
            }
            //删除分类账流水
            if (CheckRemoteServiceResultUtils.isSuccessWithData(remoteGwjFinanceService.selectGwjFinanceAccountDetailListByBizId(gwjDepositBusiness.getBizId(), SecurityConstants.INNER))) {
                if (CheckRemoteServiceResultUtils.isCodeFail(remoteGwjFinanceService.deleteGwjFinanceAccountDetailByBizId(gwjDepositBusiness.getBizId(), SecurityConstants.INNER))) {
                    throw new ServiceException("删除总账流水失败");
                }
            }
        }
        //设置日志的当前步骤
        gwjDepositBusinessLog.setStepId(gwjDepositBusiness.getStepId());
        //设置业务回退的步骤
        gwjDepositBusiness.setStepId(step.getStepId());
        //修改业务当前办理人员为上一个流程的最终提交人
        GwjUserDTO user = gwjDepositBusinessLogService.getPreviousSubmitUser(gwjDepositBusiness.getBizId(), step.getStepId());
        gwjDepositBusiness.setCurrentOperId(user.getUserId());
        gwjDepositBusiness.setCurrentOperName(user.getRealName());
        //设置回退状态
        gwjDepositBusiness.setStatus(GwjBusinessStatus.FALLBACK.getCode());
        gwjDepositBusinessMapper.updateGwjDepositBusiness(gwjDepositBusiness);
        //保存回退日志
        gwjDepositBusinessLog.setRealName(user.getRealName());
        gwjDepositBusinessLog.setUserId(user.getUserId());
        gwjDepositBusinessLogService.addFallbackLog(gwjDepositBusinessLog);
    }

    /**
     * 业务作废
     *
     * @param gwjDepositBusinessLog 业务id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void invalidBusiness(GwjDepositBusinessLog gwjDepositBusinessLog) {
        //判断当前用户是否有权限
        GwjDepositBusiness gwjDepositBusiness = checkPermission(gwjDepositBusinessLog.getBizId());
        if (checkStepConfigInfo(gwjDepositBusiness.getStepId(), StepConfigInfoEnum.INVALID.getCode())) {
            throw new ServiceException("当前步骤不允许业务作废");
        }
        //设置日志的当前步骤
        gwjDepositBusinessLog.setStepId(gwjDepositBusiness.getStepId());
        //设置作废状态
        gwjDepositBusiness.setStatus(GwjBusinessStatus.INVALID.getCode());
        //设置业务步骤为最后一步案例回顾
        List<GwjConfStep> steps = remoteGwjConfigService.selectGwjConfStepListByFlagId(gwjDepositBusiness.getBizFlagId(), SecurityConstants.INNER).getData();
        gwjDepositBusiness.setStepId(steps.get(steps.size() - 1).getStepId());
        gwjDepositBusinessMapper.updateGwjDepositBusiness(gwjDepositBusiness);
        //添加日志
        gwjDepositBusinessLogService.addInvalidBusinessLog(gwjDepositBusinessLog);
    }

    /**
     * 删除业务小区、房屋、楼栋、业主
     *
     * @param bizId 业务ID
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteCommunityBuildHouseOwnerData(Long bizId) {
        //删除业务小区
        if (StringUtils.isNotEmpty(gwjDepositBusinessCommunityService.selectGwjDepositBusinessCommunityListByBizId(bizId))) {
            gwjDepositBusinessCommunityService.deleteGwjDepositBusinessCommunityByBizId(bizId);
        }
        //删除业务楼栋
        if (StringUtils.isNotEmpty(gwjDepositBusinessBuildService.selectGwjDepositBusinessBuildListByBizId(bizId))) {
            gwjDepositBusinessBuildService.deleteGwjDepositBusinessBuildByBizId(bizId);
        }
        //删除业务房屋
        if (StringUtils.isNotEmpty(gwjDepositBusinessHouseService.selectGwjDepositBusinessHouseListByBizId(bizId))) {
            gwjDepositBusinessHouseService.deleteGwjDepositBusinessHouseByBizId(bizId);
        }
        //删除业务业主
        if (StringUtils.isNotEmpty(gwjDepositBusinessOwnerService.selectGwjDepositBusinessOwnerListByBizId(bizId))) {
            gwjDepositBusinessOwnerService.deleteGwjDepositBusinessOwnerByBizId(bizId);
        }
    }

    /**
     * 验证当前用户是否有操作权限
     *
     * @param bizId 业务ID
     * @return /
     */
    @Override
    public GwjDepositBusiness checkPermission(Long bizId) {
        Long currentUserId = SecurityUtils.getUserId();
        GwjDepositBusiness gwjDepositBusiness = gwjDepositBusinessMapper.selectGwjDepositBusinessByBizId(bizId);
        if (StringUtils.isNull(gwjDepositBusiness)) {
            throw new ServiceException("查无相关业务");
        }
        if (!currentUserId.equals(gwjDepositBusiness.getCurrentOperId()) && currentUserId != 1) {
            throw new ServiceException("当前办事员没有权限操作本业务");
        }
        return gwjDepositBusiness;
    }

    /**
     * 查询业务流程
     *
     * @param bizId     业务ID
     * @param bizFlagId 业务标识
     * @return /
     */
    @Override
    public List<GwjBusinessFlowDTO> getBusinessFlow(Long bizId, Integer bizFlagId) {
        List<GwjConfStep> steps = remoteGwjConfigService.selectGwjConfStepListByFlagId(bizFlagId, SecurityConstants.INNER).getData();
        List<GwjBusinessFlowDTO> flows = GwjConvertUtil.convertList(steps, GwjBusinessFlowDTO.class);
        if (StringUtils.isNotNull(bizId)) {
            GwjDepositBusiness gwjDepositBusiness = gwjDepositBusinessMapper.selectGwjDepositBusinessByBizId(bizId);
            try {
                flows.forEach(flow -> {
                    if (flow.getStepId().equals(gwjDepositBusiness.getStepId())) {
                        flow.setCurrent(true);
                        flow.setCurrentOperator(gwjDepositBusiness.getCurrentOperName());
                        throw new ServiceException("");
                    } else {
                        GwjUserDTO user = gwjDepositBusinessLogService.getPreviousSubmitUser(bizId, flow.getStepId());
                        if (StringUtils.isNotNull(user)) {
                            flow.setCurrentOperator(user.getRealName());
                        }
                    }
                });
            } catch (Exception ignored) {
            }
        } else {
            flows.get(0).setCurrentOperator(SecurityUtils.getLoginUser().getSysUser().getRealName());
            flows.get(0).setCurrent(true);
        }
        return flows;
    }

    /**
     * 生成流水
     *
     * @param bizId 业务主键
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean generateLedger(Long bizId) {
        // 查询业务数据
        GwjDepositBusiness business = gwjDepositBusinessMapper.selectGwjDepositBusinessByBizId(bizId);
        if (ObjectUtil.isEmpty(business)) {
            throw new ServiceException("未查询到对应业务数据");
        }
        // 查询缴费信息
        GwjDepositBusinessMoney businessMoney = gwjDepositBusinessMoneyService.selectGwjDepositBusinessMoneyByBizId(bizId);
        if (ObjectUtil.isEmpty(businessMoney)) {
            throw new ServiceException("未查询到对应缴费信息");
        }
        // 查询缴费明细
        List<GwjDepositBusinessDetail> details = gwjDepositBusinessDetailService.selectGwjDepositBusinessDetailByBizId(bizId);
        if (ObjectUtil.isEmpty(details)) {
            throw new ServiceException("未查询到对应缴费明细");
        }
        // 查询房屋信息
        List<GwjDepositBusinessHouse> houseList = gwjDepositBusinessHouseService.selectGwjDepositBusinessHouseListByBizId(bizId);
        if (ObjectUtil.isEmpty(houseList)) {
            throw new ServiceException("未查询到对应房屋信息");
        }
        // 查询银行信息
        Map<Long, GwjFinanceBank> bankMap = new HashMap<>(details.size());
        details.forEach(item -> {
            GwjFinanceBank bank = remoteGwjFinanceService.selectFinanceBankByBankIdAndAreaId(item.getBankId(), business.getAreaId(), SecurityConstants.INNER).getData();
            if (ObjectUtil.isEmpty(bank)) {
                throw new ServiceException("未查询到对应银行");
            }
            bankMap.put(bank.getBankId(), bank);
        });
        // 处理总账信息
        BigDecimal zero = new BigDecimal("0.00");
        details.forEach(item -> {
            // 设置总账参数
            GwjFinanceAccountMain accountMain = new GwjFinanceAccountMain();
            Long bankId = item.getBankId();
            accountMain.setBizId(bizId);
            accountMain.setBizNum(item.getBizNum());
            accountMain.setBizFlagId(business.getBizFlagId());
            accountMain.setBankId(bankId);
            accountMain.setBankName(bankMap.get(bankId).getBankName());
            accountMain.setBankAccount(bankMap.get(bankId).getBankAccount());
            accountMain.setBankAccountType(bankMap.get(bankId).getBankAccountType());
            if (businessMoney.getBizType() == null) {
                accountMain.setIncomeMoney(item.getDepositMoney());
                accountMain.setPayMoney(zero);
            } else {
                //退多补差总账
                GwjDepositBusinessBizTypeEnum bizTypeEnum = GwjDepositBusinessBizTypeEnum.getByCode(businessMoney.getBizType());
                if (bizTypeEnum == GwjDepositBusinessBizTypeEnum.REFUND) {
                    accountMain.setIncomeMoney(zero);
                    accountMain.setPayMoney(item.getRefundCompleteMoney());
                } else {
                    accountMain.setIncomeMoney(item.getRefundCompleteMoney());
                    accountMain.setPayMoney(zero);
                }
            }
            accountMain.setDealTime(item.getDepositTime());
            accountMain.setCheckType(GwjCheckAccountType.HANDWORK.getCode());
            accountMain.setCheckStatus(GwjCheckAccountStatus.NOT_CHECK.getCode());
            accountMain.setAreaId(business.getAreaId());
            accountMain.setDealId(item.getInvoiceNumber());
            // 插入总账信息
            if (1 != remoteGwjFinanceService.insertGwjFinanceAccountMain(accountMain, SecurityConstants.INNER).getData()) {
                throw new ServiceException("总账信息生成失败");
            }
        });
        // 处理分账信息
        ArrayList<GwjFinanceAccountDetail> detailArrayList = new ArrayList<>(houseList.size());
        detailArrayList.addAll(houseList.stream().map(item -> {
            GwjFinanceAccountDetail detail = new GwjFinanceAccountDetail();
            detail.setBizId(bizId);
            detail.setBizNum(business.getBizNum());
            detail.setBizFlagId(business.getBizFlagId());
            detail.setHouseCode(item.getHouseCode());
            if (businessMoney.getBizType() == null) {
                detail.setIncomeMoney(item.getDepositMoney());
                detail.setPayMoney(zero);
            } else {
                //退多补差分账
                GwjDepositBusinessBizTypeEnum bizTypeEnum = GwjDepositBusinessBizTypeEnum.getByCode(businessMoney.getBizType());
                if (bizTypeEnum == GwjDepositBusinessBizTypeEnum.REFUND) {
                    detail.setIncomeMoney(zero);
                    detail.setPayMoney(businessMoney.getRefundCompleteMoney());
                } else {
                    detail.setIncomeMoney(businessMoney.getRefundCompleteMoney());
                    detail.setPayMoney(zero);
                }
            }
            detail.setCheckType(GwjCheckAccountType.HANDWORK.getCode());
            detail.setCheckStatus(GwjCheckAccountStatus.NOT_CHECK.getCode());
            detail.setAreaId(business.getAreaId());
            return detail;
        }).collect(Collectors.toList()));
        // 批量插入分账信息
        if (1 != remoteGwjFinanceService.insertBatchGwjFinanceAccountDetailInner(detailArrayList, SecurityConstants.INNER).getData()) {
            throw new ServiceException("分账信息生成失败");
        }
        return Boolean.TRUE;
    }

    /**
     * 删除流水
     *
     * @param bizId 业务主键
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean removeLedger(Long bizId) {
        // 判断总账流水是否存在
        GwjFinanceAccountMain accountMain = new GwjFinanceAccountMain();
        accountMain.setBizId(bizId);
        if (ObjectUtil.isNotEmpty(remoteGwjFinanceService.selectGwjFinanceAccountMainList(accountMain, SecurityConstants.INNER).getData())) {
            // 删除总账流水
            if (0 == remoteGwjFinanceService.deleteGwjFinanceAccountMainByBizId(bizId, SecurityConstants.INNER).getData()) {
                throw new ServiceException("总账流水删除失败");
            }
        }
        // 判断是否存在分账流水
        if (ObjectUtil.isNotEmpty(remoteGwjFinanceService.selectGwjFinanceAccountDetailListByBizId(bizId, SecurityConstants.INNER).getData())) {
            // 删除分账流水
            if (0 == remoteGwjFinanceService.deleteGwjFinanceAccountDetailByBizId(bizId, SecurityConstants.INNER).getData()) {
                throw new ServiceException("分账流水删除失败");
            }
        }
        return Boolean.TRUE;
    }

    /**
     * 修改流水对账状态
     *
     * @param bizId       业主主键
     * @param checkStatus 对账状态
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean edisLedgerStatus(Long bizId, String checkStatus) {
        // 判断总账流水是否存在
        GwjFinanceAccountMain accountMain = new GwjFinanceAccountMain();
        accountMain.setBizId(bizId);
        List<GwjFinanceAccountMain> accountMainList = remoteGwjFinanceService.selectGwjFinanceAccountMainList(accountMain, SecurityConstants.INNER).getData();
        if (ObjectUtil.isEmpty(accountMainList)) {
            throw new ServiceException("未生成总账流水");
        }
        // 判断分账流水是否存在
        List<GwjFinanceAccountDetail> detailList = remoteGwjFinanceService.selectGwjFinanceAccountDetailListByBizId(bizId, SecurityConstants.INNER).getData();
        if (ObjectUtil.isEmpty(detailList)) {
            throw new ServiceException("未生成分账流水");
        }
        // 处理总账流水信息
        accountMainList = accountMainList.stream().map(item -> {
            item.setCheckStatus(checkStatus);
            item.setCheckTime(DateUtils.getNowDate());
            return item;
        }).collect(Collectors.toList());
        // 更新总账流水
        accountMainList.forEach(item -> {
            if (1 != remoteGwjFinanceService.updateGwjFinanceAccountMain(item, SecurityConstants.INNER).getData()) {
                throw new ServiceException("总账流水对账状态修改失败");
            }
        });
        // 处理分账流水信息
        ArrayList<GwjFinanceAccountDetail> accountDetailArrayList = new ArrayList<>(detailList.size());
        accountDetailArrayList.addAll(detailList.stream().map(item -> {
            item.setCheckStatus(checkStatus);
            item.setCheckTime(DateUtils.getNowDate());
            return item;
        }).collect(Collectors.toList()));
        // 批量更新分账流水
        if (0 == remoteGwjFinanceService.updateBatchGwjFinanceAccountDetail(accountDetailArrayList, SecurityConstants.INNER).getData()) {
            throw new ServiceException("分账流水对账状态修改失败");
        }
        return Boolean.TRUE;
    }

    /**
     * 查询总账列表
     *
     * @param mainRequest 查询总账列表
     * @return
     */
    @Override
    public TableDataInfo selectGwjFinanceAccountMainList(RemoteGwjFinanceAccountMainRequest mainRequest) {
        mainRequest.setPageNum(TableSupport.buildPageRequest().getPageNum());
        mainRequest.setPageSize(TableSupport.buildPageRequest().getPageSize());
        TableDataInfo tableDataInfo = remoteGwjFinanceService.selectGwjFinanceAccountMainPage(mainRequest, SecurityConstants.INNER).getData();
        if (StringUtils.isNotEmpty(tableDataInfo.getRows())) {
            List<GwjFinanceAccountMain> mains = BeanUtil.copyToList(tableDataInfo.getRows(), GwjFinanceAccountMain.class);
            mains.stream().map(item -> {
                item.setBizFlagIdStr(GwjBusinessFlagType.getInfoByCode(item.getBizFlagId()));
                item.setCheckTypeStr(GwjCheckAccountType.getInfoByCode(item.getCheckType()));
                item.setCheckStatusStr(GwjCheckAccountStatus.getInfoByCode(item.getCheckStatus()));
                return item;
            }).collect(Collectors.toList());
            tableDataInfo.setRows(mains);
        }
        return tableDataInfo;
    }

    /**
     * 查询分账列表
     *
     * @param detailRequest 分账查询参数
     * @return
     */
    @Override
    public TableDataInfo selectGwjFinanceAccountDetailList(RemoteGwjFinanceAccountDetailRequest detailRequest) {
        detailRequest.setPageNum(TableSupport.buildPageRequest().getPageNum());
        detailRequest.setPageSize(TableSupport.buildPageRequest().getPageSize());
        TableDataInfo tableDataInfo = remoteGwjFinanceService.getGwjFinanceAccountDetailPage(detailRequest, SecurityConstants.INNER).getData();
        if (StringUtils.isNotEmpty(tableDataInfo.getRows())) {
            List<GwjFinanceAccountDetail> details = BeanUtil.copyToList(tableDataInfo.getRows(), GwjFinanceAccountDetail.class);
            details.stream().map(item -> {
                item.setBizFlagIdStr(GwjBusinessFlagType.getInfoByCode(item.getBizFlagId()));
                item.setCheckTypeStr(GwjCheckAccountType.getInfoByCode(item.getCheckType()));
                item.setCheckStatusStr(GwjCheckAccountStatus.getInfoByCode(item.getCheckStatus()));
                return item;
            }).collect(Collectors.toList());
            tableDataInfo.setRows(details);
        }
        return tableDataInfo;
    }

    /**
     * 查询对账方式
     *
     * @return
     */
    @Override
    public Map<String, String> getCheckAccountType() {
        Map<String, String> checkAccountTypeMap = new HashMap<>();
        for (GwjCheckAccountType value : GwjCheckAccountType.values()) {
            checkAccountTypeMap.put(value.getCode(), value.getInfo());
        }
        return checkAccountTypeMap;
    }

    /**
     * 查询对账状态
     *
     * @return
     */
    @Override
    public Map<String, String> getCheckAccountStatus() {
        Map<String, String> checkAccountTypeMap = new HashMap<>();
        for (GwjCheckAccountStatus value : GwjCheckAccountStatus.values()) {
            checkAccountTypeMap.put(value.getCode(), value.getInfo());
        }
        return checkAccountTypeMap;
    }

    /**
     * 校验步骤
     *
     * @param stepId         步骤主键
     * @param stepConfigInfo 操作信息
     */
    private Boolean checkStepConfigInfo(Long stepId, String stepConfigInfo) {
        List<String> configInfos = remoteGwjConfigService.selectGwjConfStepConfigInfoCodeListByBizFlagId(stepId, SecurityConstants.INNER).getData();
        if (!StringUtils.containsAny(configInfos, stepConfigInfo)) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }
}
